package managedbean.IT;

import javax.faces.event.ActionEvent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;
import session.stateless.AccessRoleSessionBeanLocal;
import javax.faces.bean.ManagedProperty;
import managedbean.UserSessionManagerBean;
import entity.AccessRole;
import java.util.ArrayList;
import javax.faces.context.FacesContext;
import session.stateless.LogSessionBeanLocal;

@ManagedBean(name = "roleManagerBean")
@RequestScoped
public class RoleManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private AccessRoleSessionBeanLocal accessRoleSessionBeanLocal;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    
    private String loadAddRole;
    private String roleType;
    private String description;
    private String loadUpdateRole;
    private String loadSearchRole;
    private static AccessRole role;
    private ArrayList<AccessRole> roles;
    private String request;

    //Page onload
    public String getLoadUpdateRole() {

        try {
            role = (AccessRole) userSessionManagerBean.getSelectedItem();
            roleType = role.getRoleType();
            description = role.getDescription();
        } catch (Exception exception) {
            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_IT/searchRole.xhtml?request=updateRole");
            } catch (Exception ex) {
            }
        }



        return null;
    }

    public String getLoadSearchRole() {

        String temp = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("request");

        if (temp != null) {
            userSessionManagerBean.setSelectedItem((String) temp + ".xhtml");

            try {


                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_IT/searchRole.xhtml");

            } catch (Exception ex) {
            }
        }

        request = (String) userSessionManagerBean.getSelectedItem();
        roles = accessRoleSessionBeanLocal.getAccessRoles();
        return null;
    }

// <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">
    /**
     * @return the role
     */
    public AccessRole getRole() {
        return role;
    }

    /**
     * @param role the role to set
     */
    public void setRole(AccessRole role) {
        this.role = role;
    }

    /**
     * @return the roles
     */
    public ArrayList<AccessRole> getRoles() {
        return roles;
    }

    /**
     * @param roles the roles to set
     */
    public void setRoles(ArrayList<AccessRole> roles) {
        this.roles = roles;
    }

    public String getLoadAddRole() {
        return loadAddRole;
    }

    public void setLoadAddRole(String loadAddRole) {
        this.loadAddRole = loadAddRole;
    }

    public void setLoadUpdateRole(String loadUpdateRole) {
        this.loadUpdateRole = loadUpdateRole;
    }

    public String getRoleType() {
        return roleType;
    }

    public void setRoleType(String roleType) {
        this.roleType = roleType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    public AccessRoleSessionBeanLocal getAccessRoleSessionBeanLocal() {
        return accessRoleSessionBeanLocal;
    }

    public void setAccessRoleSessionBeanLocal(AccessRoleSessionBeanLocal accessRoleSessionBeanLocal) {
        this.accessRoleSessionBeanLocal = accessRoleSessionBeanLocal;
    }

    public void setLoadSearchRole(String loadSearchRole) {

        this.loadSearchRole = loadSearchRole;

    }

//</editor-fold>
    public void addNewRole(ActionEvent event) {
        accessRoleSessionBeanLocal.addRole(getRoleType(), getDescription());
        
        logSessionBeanLocal.addLog("Add access role", "Access role created: " + getRoleType(),"addRole.xhtml", userSessionManagerBean.getUserId());

        ArrayList<String> al = new ArrayList<String>();
        al.add("Access role created");
        al.add("You have successfully created a new role. Please go to Assign role page to assign the role to the user account.");
        al.add("Click on the link to return to ");
        al.add("continue");
        al.add("Web_IT/addRole.xhtml");
        userSessionManagerBean.setSelectedItem(al);


        try {

            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort()
                    + "/MerlionERP-war/success.xhtml");
        } catch (Exception ex) {
        }
    }

    public void removeRole(ActionEvent event) {

        try {

            accessRoleSessionBeanLocal.deleteRole(roleType);
            logSessionBeanLocal.addLog("Delete access role", "Access role removed: " + getRoleType(),"updateRole.xhtml", userSessionManagerBean.getUserId());
            ArrayList<String> al = new ArrayList<String>();
            al.add("Access role removed");
            al.add("You have successfully removed the access role. The user can no longer grant access given to this role.");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("Web_IT/searchRole.xhtml?request=updateRole");
            userSessionManagerBean.setSelectedItem(al);
            
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort()
                    + "/MerlionERP-war/success.xhtml");
        } catch (Exception ex) {
        }
    }

    public void doneUpdate(ActionEvent event) {
        try {
             accessRoleSessionBeanLocal.updateRole(role.getRoleType(), description);
            logSessionBeanLocal.addLog("Update access role", "Access role updated: " + getRoleType(),"updateRole.xhtml", userSessionManagerBean.getUserId());
       
             ArrayList<String> al = new ArrayList<String>();
            al.add("Access role updated");
            al.add("You have successfully updated the access role. ");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("Web_IT/searchRole.xhtml?request=updateRole");
            userSessionManagerBean.setSelectedItem(al);

            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/success.xhtml");
        } catch (Exception ex) {
        
        }
    }

    public void edit(ActionEvent event) {
        userSessionManagerBean.setSelectedItem((AccessRole) role);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_IT/" + request);
        } catch (Exception ex) {
        }
    }
}
